$(document).ready(function() {
	var citydata = returnCitySN;
	console.log(citydata.cname);
	$("#citytext").text(citydata.cname);
	//返回对象Object {cip: "122.70.200.146", cid: "110000", cname: "北京市"}
	getdata();
	getmouth();
	getyear();
	getElectricity();
	getWatar();
	water();
});

//获取环境数据
var getdata = (function() {
	var dataurl = environmentport + '/ScreenController/getEnvironmentCurrentData';
	$.get(dataurl, function(result) {
		var code = result.code;
		var list = result.object;
		console.log(result);
		if(code == 0) {
			$.each(list, function(idx, obj) {
				var idx = idx + 1;
				$("#humidity").text("60" + "%rh");//obj.humidity
				$("#temperature").text("15" + "℃");//obj.temperature
				$("#windspeed").text(obj.windspeed + "m/s");
				$("#winddirection").text("西北风");//obj.winddirection
				var pm25 = obj.pm25;
				var pm10 = obj.pm10;
				var windpower = obj.windpower;
				var noise = obj.noise;
				setTimeout(function() {
					cavasmonitor(pm25, pm10, windpower, noise);
				}, 300);
			})
		}
	})
})

//本月环境数据
var getmouth = (function() {
	var mouthurl = environmentport + '/ScreenController/getDevice';
	var mouthnoiseurl = environmentport + '/ScreenController/getNoiselevelByMonth';
	$.get(mouthurl, function(result) {
		var code = result.code;
		var list = result.object;
		console.log(result);
		if(code == 0) {
			$("#perfect").text(list.perfect);
			$("#good").text(list.good);
			$("#slight").text(list.slight);
			$("#middle").text(list.middle);
			$("#heavy").text(list.heavy);
			$("#serious").text(list.serious);
			var perfect = list.perfect;
			var good = list.good;
			var slight = list.slight;
			var middle = list.middle;
			var heavy = list.heavy;
			var serious = list.serious;
			//噪音
			$.get(mouthnoiseurl, function(result) {
				var code = result.code;
				var listn = result.object;
				console.log(result);
				if(code == null) {
					$("#qualified").text(listn.qualified);
					$("#unqualified").text(listn.unqualified);
					var qualified = listn.qualified;
					var unqualified = listn.unqualified;
					//噪音
					setTimeout(function() {
						cavasm(perfect, good, slight, middle, heavy, serious, qualified, unqualified);
					}, 300);

				}
			})
		}
	})
})

//本年度环境数据
var getyear = (function() {
	var yearthurl = environmentport + '/ScreenController/getAQIlevelByYear';
	var yearnoiseurl = environmentport + '/ScreenController/getNoiselevelByYear';
	$.get(yearthurl, function(result) {
		var code = result.code;
		var list = result.object;
		console.log(result);
		if(code == 0) {
			$("#perfect1").text(list.perfect);
			$("#good1").text(list.good);
			$("#slight1").text(list.slight);
			$("#middle1").text(list.middle);
			$("#heavy1").text(list.heavy);
			$("#serious1").text(list.serious);
			var p = list.perfect;
			var g = list.good;
			var s = list.slight;
			var m = list.middle;
			var he = list.heavy;
			var se = list.serious;
			//噪音
			$.get(yearnoiseurl, function(result) {
				var code = result.code;
				var listn = result.object;
				console.log(result);
				if(code == null) {
					$("#qualified1").text(listn.qualified);
					$("#unqualified1").text(listn.unqualified);
					var qu = listn.qualified;
					var unq = listn.unqualified;
					//噪音
					setTimeout(function() {
						cavasy(p, g, s, m, he, se, qu, unq);
					}, 300);

				}
			})
		}
	})
})

//获取总用电量
var getElectricity = (function() {
	var electricityurl = environmentport + '/ScreenController/getElectricityConsumption';
	$.get(electricityurl, function(result) {
		var code = result.code;
		var list = result.object;
		if(code == 0) {
			var quarters = list.quarters;
			var office = list.office;
			var construction = list.construction;
			setTimeout(function() {
				cavaselectric(quarters, office, construction);
			}, 300);
		}
	})
})

//获取总用水量
var getWatar = (function() {
	var watarurl = environmentport + '/ScreenController/getWaterConsumption';
	$.get(watarurl, function(result) {
		var code = result.code;
		var list = result.object;
		if(code == 0) {
			var quarters = list.quarters;
			var office = list.office;
			var construction = list.construction;
			setTimeout(function() {
				cavaswater(quarters, office, construction);
			}, 300);
		}
	})
})

function water() {
		/*计算一个segment的宽度*/
		var segmentWidth = 20;
		$("#container #content li").each(function() {
			segmentWidth += $(this).outerWidth(true);
		});

		$("#container #content li").clone().appendTo($("#container #content"));

		run(60000);

		function run(interval) {
			$("#container #content").animate({ "left": -segmentWidth }, interval, "linear", function() {
				$("#container #content").css("left", 0);
				run(60000);
			});
		}

		$("#container").mouseenter(function() {
			$("#container #content").stop();
		}).mouseleave(function() {
			var passedCourse = -parseInt($("#container #content").css("left"));
			var time = 60000 * (1 - passedCourse / segmentWidth);
			run(time);
		});
	}